package uk.co.aetherworks.osc_messaging;

/*
 * Ped-ome
 *
 * AppStatusInterpreter - Interprets SerialOSC status messages received from a monome app
 *
 * Joe Handford aka Knecht 2011
 *
 */ 

// OSC Include Files
import com.illposed.osc.OSCPortOut;
import com.illposed.osc.OSCPortIn;
import com.illposed.osc.OSCListener;
import com.illposed.osc.OSCMessage;

// Java Net Include Files

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

// aetherworks imports
import uk.co.aetherworks.debug.*;

public class AppStatusInterpreter extends Debuggable implements OSCListener {

	// The object that will process the command in order to update Pedome's gui
	OSCCmdHandler cmd_processor;
	// The object that will forward the command to the monome
	PedToMon forwarder;
	AppStatusListener myOwner;
	
	int app_listening_port = 0;
	String appPrefix = "/pedome";
	String monPrefix = "";

	public AppStatusInterpreter(OSCCmdHandler handler, AppStatusListener owner){
		// Reference the command handlers
		cmd_processor = handler;
		myOwner = owner;
	}
	
	public void acceptMessage(java.util.Date time, OSCMessage message){
		int x, y, state;
//		int app_listening_port;
		String cmd = "do nothing";

		String address = message.getAddress();
		Object myArgs[] = message.getArguments();
		
		if ("/sys/port".equals(address)) {
			DEBUG("/sys/port received from app");
			DEBUG("    num args: " + myArgs.length);
			// Get app listening port from message
			if (myArgs.length == 2) {
				DEBUG("    2 args");
				app_listening_port = ((Integer) myArgs[1]).intValue();
			}
			else {
				DEBUG("    1 args");
				app_listening_port = ((Integer) myArgs[0]).intValue();				
			}
			// Wire up step c1:  Set ped_to_app_port.
			DEBUG("c1. Setting ped_to_app_port to: " + app_listening_port);
			cmd_processor.setCmdListeningPort(app_listening_port);
		}
		else if ("/sys/info".equals(address)) {
			DEBUG("/sys/info received from app");
			// Wire up step d1:  Send status to App.
			DEBUG("d1. Sending status to App on port: " + app_listening_port);
			cmd_processor.sendPedomeStatus(app_listening_port);
		}
		else if ("/sys/prefix".equals(address)) {
			String appPrefix = ((String) myArgs[0]).toString();				
			DEBUG("/sys/prefix " + appPrefix + " received from app");
			cmd_processor.setAppPrefix(appPrefix);
		}
		else if ((appPrefix + "/grid/led/set").equals(address)) {
			x = ((Integer) myArgs[0]).intValue();
			y = ((Integer) myArgs[1]).intValue();
			state = ((Integer) myArgs[2]).intValue();
			DEBUG(appPrefix + "/grid/led/set received.");
			
			cmd_processor.processCmd(x, y, state);
			message.setAddress(monPrefix + "/grid/led/set");
			forwarder.fwdMsg(message);
		}
	}
	
	public void attachForwarder(PedToMon aForwarder) {
		forwarder = aForwarder;
		myOwner.updateListeners();
	}
	
	public void detachForwarder() {
		forwarder = null;
	}

	public void setAppPrefix(String prefix) {
		appPrefix = prefix;
	}
	
	public void setMonPrefix(String prefix) {
		monPrefix = prefix;
	}
}
